package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/6/13 22:13
 */
public class C61 {

    public ListNode rotateRight(ListNode head, int k) {
        if(head == null) return null;
        int N = 0;
        ListNode fast = head, slow = head;
        while (fast != null) {
            N++;
            fast = fast.next;
        }
        k = k % N;
        if(N == 1 || N == k || k == 0) return head;
        fast = head;
        for (int i = 0; i < k; i++) {
            fast = fast.next;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        ListNode res = slow.next;
        slow.next = null;
        fast.next = head;
        return res;
    }

}
